<template>
  <div>
    <el-dialog
      :title="$t('DefaultCustomerPermissionsLang.select_customer')"
      :visible.sync="Visible"
      width="1200px"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <div class="midContant clearfix">
         <FilterBox
          :labelWidth="120"
          :filterItemList="filterItemList"
          :filterData="filterData"
          @search="search"
          @clear="clear"
          @keep="keepHeaderList"
        >
        </FilterBox>
        <MyTable
          style="height:500px"
          ref="roleCustomer_add"
          :gridOptions="{checkboxConfig:{checkField: 'checked', trigger: 'row', highlight: true,reserve:true},rowConfig:{isCurrent: true, isHover: true,keyField:'id'}}"
          :tableColumn="tableColumn"
          :tableData="tableData"
          :isOrder="false"
          @sendCheckBoxList="setCheckBox"
          >
            <template v-slot:content="scope">
              <span>{{ scope.row[scope.config.prop] }}</span>
            </template>
        </MyTable>
        <Pagination
          :isHaveBtn="false"
          :currentPage="pageinfo.page"
          :pageSize="pageinfo.limit"
          :total="pageinfo.number_all"
          :page_all="pageinfo.page_all"
          @pagination="SetPagination"
        >
        </Pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm" size="small"
          >{{$t('common.confirm')}}</el-button
        >
        <el-button @click="cancel" size="small">{{$t('common.cancel')}}</el-button>
      </span>
      
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {  },
  props: {
    // 接收绑定参数
    
    midHight: [Number,String],
  },
  data() {
    return {
      filterItemList:[
        // input输入框
        { label:this.$t('CustomerManagementLang.Name'),width:200, filterKey: 'keyword', filterType: 'SearchInput', placeholderStr: null },
        // input输入框
        { label:this.$t('CustomerManagementLang.code'),width:200, filterKey: 'nickname', filterType: 'SearchInput', placeholderStr: null },
        // input输入框
        { label:this.$t('CustomerManagementLang.Telephone'),width:200, filterKey: 'tel', filterType: 'SearchInput', placeholderStr: null },
      ],
      filterData:{
        page: "",
        limit: "30",
        order: "",
        
        keyword: "",
        nickname: "",
        tel: "",
      },
      role_id:'',
      Visible: false,
      tableColumn: [
        {
          label: "",
          prop: "table_code",
          width:40,
        },
        {
          type: 'checkbox',
          width: 40,
        },
        {
          checked: true,
          label: this.$t('CustomerManagementLang.Name'),
          prop: "name",
          width: 160,
          order:"",
        },
        {
          checked: true,
          label: this.$t('CustomerManagementLang.code'),
          prop: "nickname",
          width: 160,
          order:"",
        },
        {
          checked: true,
          label: this.$t('CustomerManagementLang.Telephone'),
          prop: "mobile",
          width: 120,
          order:"",
        },
        {
          checked: true,
          label: this.$t('CustomerManagementLang.email'),
          prop: "email",
          width: 160,
          order:"",
        },
        {
          checked: true,
          label: this.$t('ExpressOrderManagementLang.sale'),
          prop: "belong_sale",
          width: 80,
          order:"",
        },
        {
          checked: true,
          label: this.$t('CustomerManagementLang.discount'),
          prop: "discount",
          width: 80,
          order:"",
        },
      ],
      tableData: [],
      sendDataArrCopy:[],
      sendDataArr:[],
      pageinfo: {
        page: 1,
        page_all: 1,
        limit: 30,
        number_all: 0,
      },
      rows:[]
    };
  },
  created() {
    
  },
  mounted() {
    },
  watch: {
    },
  methods: {
    init(data,rows) {
      this.role_id=data
      this.Visible = true;
      this.rows=rows
      this.sendDataArrCopy=JSON.parse(JSON.stringify(this.rows))
      this.getTableData();
    },
    //查询
    search(){
      this.getTableData()
    },
    //清空
    clear(){
      this.filterData.keyword= "",
      this.filterData.nickname= "",
      this.filterData.tel= "",
      this.getTableData()
    },
    // 获取表头组
    getHeaderList(){
      this.$get("get_th",{menu_id:this.tabsId}).then((res) => {
        if(!!res.data.th_json){
          this.tableColumn=JSON.parse(res.data.th_json)
        }
      });
    },
    //保存表头表头组
    keepHeaderList(){
        let params = new FormData();
        params.append("menu_id", this.tabsId);
        params.append("th_json", JSON.stringify(this.tableColumn));
        this.$postForm("save_th", params).then((res) => {
          
        });
    },
    //获取表格数据
    async getTableData(){
      let arr=[]
      await this.$get('sonMembers', this.filterData).then((res) => {
        this.tableData=res.data
        this.pageinfo = res.pageinfo;
        this.pageinfo.limit=Number(this.pageinfo.limit)
        this.rows.forEach(el=>{
          if(res.data.filter(item=>{return item.uid==el.uid}).length>0){
            arr.push(res.data.filter(item=>{return item.uid==el.uid})[0])
          }
        })
      })
      console.log(arr)
      this.toggleSelection(arr)
    },
    // 点击排序
    setOrder(data){
      this.filterData.order=data
      this.getTableData()
    },
    // 更改分页
    SetPagination(data){
      this.filterData.page=data.page
      this.filterData.limit=data.limit
      this.pageinfo.page=data.page
      this.pageinfo.limit=data.limit
      this.getTableData()
    },
    // 多选框勾选集合
    setCheckBox(data){
      this.sendDataArr=data
      this.sendDataArrCopy=this.sendDataArrCopy.filter(item=>{return this.tableData.findIndex(el=>{return el.id==item.id})==-1})
      this.sendDataArrCopy.push(...this.sendDataArr)
    },
    //默认勾选
    toggleSelection(rows) {
      if (!!rows&&rows.length>0) {
        rows.forEach(row => {
          this.$refs.roleCustomer_add.$refs.tableRef.setCheckboxRow(row,true);
        });
        this.sendDataArr=this.tableData.filter(item=>{return item.checked})
      } else {
        this.$refs.roleCustomer_add.$refs.tableRef.clearCheckboxRow();
        this.sendDataArr=[]
      }
    },
    // 提交修改
    confirm() {
      console.log(this.sendDataArrCopy)
      //是否有空值必选项标识
      let type=true 
      if(type){
        let arr=this.sendDataArrCopy.map((obj)=>{return ""+obj.uid})
        let params = new FormData();
          params.append("role_id", this.role_id);
          params.append("uids", JSON.stringify(arr));
        this.$postForm('addRoleMembers',params).then((res) => {
          if(res.success){
            this.cancel()
            this.$message.success(res.msg);
            this.$emit("success");
          }
        });
      }
    },
    //取消修改
    cancel(){
      this.filterData={
        page: "",
        limit: "30",
        order: "",
        
        keyword: "",
        nickname: "",
        tel: "",
      }
      this.toggleSelection()
      this.Visible = false;
    },
  },
};
</script>

<style scoped lang="less" >

</style>